<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns:MSHelp="http://www.microsoft.com/MSHelp/" lang="en-us" xml:lang="en-us"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="DC.Type" content="topic">
<meta name="DC.Title" content="Dependency Flow Graph Example">
<meta name="DC.subject" content="Dependency Flow Graph Example">
<meta name="keywords" content="Dependency Flow Graph Example">
<meta name="DC.Relation" scheme="URI" content="../../reference/flow_graph.htm">
<meta name="DC.Relation" scheme="URI" content="continue_msg_cls.htm">
<meta name="DC.Format" content="XHTML">
<meta name="DC.Identifier" content="dependency_flow_graph_example">
<meta name="DC.Language" content="en-US">
<link rel="stylesheet" type="text/css" href="../../intel_css_styles.css">
<title>Dependency Flow Graph Example</title>
</head>
<body id="dependency_flow_graph_example">
 <!-- ==============(Start:NavScript)================= -->
 <script src="..\..\NavScript.js" language="JavaScript1.2" type="text/javascript"></script>
 <script language="JavaScript1.2" type="text/javascript">WriteNavLink(2);</script>
 <!-- ==============(End:NavScript)================= -->
<a name="dependency_flow_graph_example"><!-- --></a>

 
  <h1 class="topictitle1">Dependency Flow Graph Example</h1>
 
   
  <div> 
	 <p>In the following example, five computations A-E are set up with the
		partial ordering shown below in "A simple dependency graph.". For each edge in
		the flow graph, the node at the tail of the edge must complete its execution
		before the node at the head may begin. 
	 </p>
 
	 <div class="Note"><h3 class="NoteTipHead">
					Note</h3> 
		<p>This is a simple syntactic example only. Since each node in a flow
		  graph may execute as an independent task, the granularity of each node should
		  follow the general guidelines for tasks as described in Section 3.2.3 of the
		  Intel&reg; Threading Building Blocks Tutorial. 
		</p>
 
	 </div> 
	 <div class="fignone" id="fig5_dep_graph"><a name="fig5_dep_graph"><!-- --></a><span class="figcap">A simple dependency graph.</span> 
		 
		<br><a name="image_45FEEBCB0EBD40C19CCD9FDB0E503A15"><!-- --></a><div class="imagecenter"><img id="image_45FEEBCB0EBD40C19CCD9FDB0E503A15" src="../Resources/dep_graph.jpg" height="409" width="249" alt="A simple dependency graph." align="center"></div><br> 
	 </div>
 
	 <pre>#include &lt;cstdio&gt;
#include "tbb/flow_graph.h"
&nbsp;
using namespace tbb::flow;
&nbsp;
struct body {
&nbsp;&nbsp;&nbsp; std::string my_name;
&nbsp;&nbsp;&nbsp; body( const char *name ) : my_name(name) {}
&nbsp;&nbsp;&nbsp; void operator()( continue_msg ) const {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%s\n", my_name.c_str());
&nbsp;&nbsp;&nbsp; }
};
&nbsp;
int main() {
&nbsp;&nbsp;&nbsp; graph g;
&nbsp;
&nbsp;&nbsp;&nbsp; broadcast_node&lt; continue_msg &gt; start;
&nbsp;&nbsp;&nbsp; continue_node&lt;continue_msg&gt; a( g, body("A"));
&nbsp;&nbsp;&nbsp; continue_node&lt;continue_msg&gt; b( g, body("B"));
&nbsp;&nbsp;&nbsp; continue_node&lt;continue_msg&gt; c( g, body("C"));
&nbsp;&nbsp;&nbsp; continue_node&lt;continue_msg&gt; d( g, body("D"));
&nbsp;&nbsp;&nbsp; continue_node&lt;continue_msg&gt; e( g, body("E"));
&nbsp;
&nbsp;&nbsp;&nbsp; make_edge( start, a );
&nbsp;&nbsp;&nbsp; make_edge( start, b );
&nbsp;&nbsp;&nbsp; make_edge( a, c );
&nbsp;&nbsp;&nbsp; make_edge( b, c );
&nbsp;&nbsp;&nbsp; make_edge( c, d );
&nbsp;&nbsp;&nbsp; make_edge( a, e );
&nbsp;
&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; 3; ++i ) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start.try_put( continue_msg() );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g.wait_for_all();
&nbsp;&nbsp;&nbsp; }
&nbsp;
&nbsp;&nbsp;&nbsp; return 0;
}&nbsp;&nbsp;</pre>
	 <p>In this example, nodes A-E print out their names. All of these nodes are
		therefore able to use 
		<span class="keyword">struct body</span> to construct their body objects. 
	 </p>
 
	 <p>In function 
		<span class="keyword">main</span>, the flow graph is set up once and then run three
		times. All of the nodes in this example pass around 
		<span class="keyword">continue_msg</span> objects. This type is used to communicate
		that a node has completed its execution. 
	 </p>
 
	 <p>The first line in function 
		<span class="keyword">main</span> instantiates a 
		<span class="keyword">graph</span> object, 
		<span class="keyword">g</span>. On the next line, a 
		<span class="keyword">broadcast_node</span> named 
		<span class="keyword">start</span> is created. Anything passed to this node will be
		broadcast to all of its successors. The node 
		<span class="keyword">start</span> is used in the 
		<span class="keyword">for</span> loop at the bottom of 
		<span class="keyword">main</span> to launch the execution of the rest of the flow
		graph. 
	 </p>
 
	 <p>In the example, five 
		<span class="keyword">continue_node</span> objects are created, named a - e. Each
		node is constructed with a reference to 
		<span class="keyword">graph</span> 
		<span class="keyword">g</span> and the function object to invoke when it runs. The
		successor / predecessor relationships are set up by the 
		<span class="keyword">make_edge</span> calls that follow the declaration of the
		nodes. 
	 </p>
 
	 <p>After the nodes and edges are set up, the 
		<span class="keyword">try_put</span> in each iteration of the 
		<span class="keyword">for</span> loop results in a broadcast of a 
		<span class="keyword">continue_msg</span> to both 
		<span class="keyword">a</span> and 
		<span class="keyword">b</span>. Both 
		<span class="keyword">a</span> and 
		<span class="keyword">b</span> are waiting for a single 
		<span class="keyword">continue_msg</span>, since they both have only a single
		predecessor, 
		<span class="keyword">start</span>. 
	 </p>
 
	 <p>When they receive the message from 
		<span class="keyword">start</span>, they execute their body objects. When complete,
		they each forward a 
		<span class="keyword">continue_msg</span> to their successors, and so on. The graph
		uses tasks to execute the node bodies as well as to forward messages between
		the nodes, allowing computation to execute concurrently when possible. 
	 </p>
 
	 <p>The classes and functions used in this example are described in detail
		in topics linked from the Flow Graph parent topic. 
	 </p>
 
  </div>
 
  
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong>&nbsp;<a href="../../reference/flow_graph.htm">Flow Graph</a></div>
</div>
<div class="See Also">
<h2>See Also</h2>
<div class="linklist">
<div><a href="continue_msg_cls.htm">continue_msg Class
		  </a></div></div>
</div>

</body>
</html>
